﻿//-----------------------------------------------------------------------------
// <copyright file="OnyxContainer.cs" company="William E. Kempf">
//     Copyright (c) William E. Kempf.
// </copyright>
//-----------------------------------------------------------------------------

namespace Onyx.Windows
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// Defines a container for services.
    /// </summary>
    /// <remarks>
    /// Silverlight doesn't provide a ServiceContainer type, so this provides an alternative.
    /// </remarks>
    public class OnyxContainer : IServiceProvider
    {
        /// <summary>
        /// The registered services.
        /// </summary>
        private readonly Dictionary<Type, object> services = new Dictionary<Type, object>();

        /// <summary>
        /// Gets the service object of the specified type.
        /// </summary>
        /// <param name="serviceType">An object that specifies the type of service object to get.</param>
        /// <returns>
        /// A service object of type <paramref name="serviceType"/>.
        /// -or-
        /// null if there is no service object of type <paramref name="serviceType"/>.
        /// </returns>
        public object GetService(Type serviceType)
        {
            object service = null;
            this.services.TryGetValue(serviceType, out service);
            return service;
        }

        /// <summary>
        /// Adds the service.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="service">The service.</param>
        public void AddService(Type serviceType, object service)
        {
            this.services.Add(serviceType, service);
        }
    }
}
